package br.com.cocodonto.modelo.dao;

import br.com.cocodonto.framework.dao.CreateDaoException;
import br.com.cocodonto.framework.dao.DaoHelper;
import br.com.cocodonto.modelo.entidade.Paciente;

import java.sql.*;


public class PacienteDao {
	
	private DaoHelper daoHelper;
	
	public PacienteDao() {
	    daoHelper = new DaoHelper();
	}
	
	
	public void inserir(Paciente paciente) throws CreateDaoException {
		
		EnderecoDAO enderecodao = new EnderecoDAO();
		Connection conn = null;
		PreparedStatement stmt = null;
		
		try {
		    
			daoHelper.beginTransaction();
			
			conn = daoHelper.getConnectionFromContext();
		    int index =0;
		    
            stmt = conn.prepareStatement("insert into paciente (nome, rg, cpf, sexo) values (? , ? , ? , ?)");
            
            stmt.setString(++index, paciente.getNome());
            stmt.setString(++index, paciente.getRg());
            stmt.setString(++index, paciente.getCpf());
            stmt.setString(++index, paciente.getSexo().toString());
                     
            stmt.executeUpdate();
            
            enderecodao.insert(paciente.getEndereco());
            
            daoHelper.endTransaction();

		
		} catch (SQLException e) {
		     e.printStackTrace();
		}finally {
			daoHelper.releaseAll(stmt, conn);
			
		}
		

	}
	
	private void inserirPacienteEndereco(Paciente paciente) {
		
		EnderecoDAO enderecodao = new EnderecoDAO();

		
		}

}
